package controller;

import java.util.Calendar;

import utils.Utils;
import android.content.Context;
import android.util.Log;
import app.fitness.fragment.DashBoardDetailFragment;
import app.fitness.fragment.DashBoardFragment;

import com.razer.android.nabuopensdk.NabuOpenSDK;
import com.razer.android.nabuopensdk.interfaces.SleepHistoryListener;
import com.razer.android.nabuopensdk.interfaces.SleepTrackerListener;
import com.razer.android.nabuopensdk.models.NabuSleepHistory;
import com.razer.android.nabuopensdk.models.NabuSleepTracker;
import com.razerzone.android.fitness.ActivityFitness;
import com.razerzone.android.fitness.R;

import define.Constants;
import define.DashBoard;
import define.DashBoardDetail;
import define.Key;
import define.SleepData;

public class GetSleepData {
	/**
	 * Data section
	 */
	public static boolean[] sleep_daily_value = new boolean[1440 * 3];

	public static int[] size_today = null;
	public static int[] size_starttime_today = null;
	public static int[] size_today_endtime = null;

	/**
	 * String section
	 */
	public static int size_actual_today = 0;
	private static int KEY = 0;

	/**
	 * View section
	 */

	/**
	 * The others section
	 */
	private static Context context;

	private static void addSleepValue(NabuSleepTracker[] nabuSleepTracker,
			NabuSleepHistory[] nabuSleepHistory) {
		switch (KEY) {
		case Key.SLEEP_DATA:
			if (nabuSleepTracker == null) {
				return;
			}

			/**
			 * Get size
			 */
			for (NabuSleepTracker data : nabuSleepTracker) {
				Calendar mCalendarStart = Calendar.getInstance();
				mCalendarStart.setTimeInMillis(data.startTime * 1000);

				Calendar mCalendarEnd = Calendar.getInstance();
				mCalendarEnd.setTimeInMillis(data.endTime * 1000);

				// Log.i("", "startTime " +
				// Utils.showDate(mCalendarStart.getTime()));
				// Log.i("", "endTime " +
				// Utils.showDate(mCalendarEnd.getTime()));
			}

			/**
			 * Set up the size of Daily Sleep data = 1440*3 Initialize size
			 */
			size_actual_today = 0;
			size_starttime_today = new int[nabuSleepTracker.length];
			size_today = new int[nabuSleepTracker.length];
			size_today_endtime = new int[nabuSleepTracker.length];

			/**
			 * Get data
			 */
			int index_in_size = 0;
			for (NabuSleepTracker data : nabuSleepTracker) {
				if (!SleepData.mLlSdRecordStart.contains(data.startTime)) {
					/**
					 * Put sleep data into specified array to get correct time,
					 * included hours & minutes
					 */
					/**
					 * Convert from GMT time to Local time & Add record time
					 */
					SleepData.mLlSdRecordStart.add(data.startTime);
					SleepData.mLlSdRecordEnd.add(data.endTime);

					Calendar mCalendarStart = Calendar.getInstance();
					mCalendarStart.setTimeInMillis(data.startTime * 1000);
					Calendar mCalendarEnd = Calendar.getInstance();
					mCalendarEnd.setTimeInMillis(data.endTime * 1000);

					/**
					 * Get current day to compare
					 */
					int[] today = getTodayFromCurrentDate();
					if ((mCalendarStart.get(Calendar.DAY_OF_MONTH) < today[0] & mCalendarStart
							.get(Calendar.MONTH) == today[1])
							| (mCalendarStart.get(Calendar.DAY_OF_MONTH) > today[0] & mCalendarStart
									.get(Calendar.MONTH) < today[1])) {
						/**
						 * If start day [i] < today, draw from end time[0] to 0
						 * AM
						 */
						/**
						 * If data returned is different day, used today, so
						 * need draw from 0:00 AM
						 */
						size_starttime_today[index_in_size] = data.data.size();
						index_in_size++;

						/**
						 * Decrease size if is not today
						 */
						size_actual_today--;
					} else if ((today[0] < mCalendarEnd
							.get(Calendar.DAY_OF_MONTH) & mCalendarEnd
							.get(Calendar.MONTH) == today[1])
							| (today[0] > mCalendarEnd
									.get(Calendar.DAY_OF_MONTH) & mCalendarEnd
									.get(Calendar.MONTH) > today[1])) {
						/**
						 * If today < end time[i], draw from start time[i] to 0
						 * AM
						 */
						size_today_endtime[index_in_size] = data.data.size();
						index_in_size++;

						/**
						 * Decrease size if is not today
						 */
						size_actual_today--;
					} else if (mCalendarStart.get(Calendar.DAY_OF_MONTH) == today[0]
							& mCalendarStart.get(Calendar.MONTH) == today[1]) {
						/**
						 * If start day [i] = today, draw from start time[i] to
						 * end time[i]
						 */
						/**
						 * If data returned is same day, so need draw from
						 * specify hours
						 */
						/**
						 * Check if be same day, should add item into size of
						 * today first
						 */
						if (mCalendarStart.get(Calendar.DAY_OF_MONTH) == mCalendarEnd
								.get(Calendar.DAY_OF_MONTH)) {
							size_today[index_in_size] = data.data.size();
							index_in_size++;

							/**
							 * Increase size if is not today
							 */
							size_actual_today++;
						}
					}

					int hours_start_time = mCalendarStart
							.get(Calendar.HOUR_OF_DAY);
					int minutes_start_time = mCalendarStart
							.get(Calendar.MINUTE);

					/**
					 * Put sleep data into specified array to draw on chart
					 */
					int index_start_time = hours_start_time * 60
							+ minutes_start_time;
					for (int i = 0; i < data.data.size(); i++) {
						/**
						 * Check day condition to do so far
						 */
						if ((mCalendarStart.get(Calendar.DAY_OF_MONTH) < today[0] & mCalendarStart
								.get(Calendar.MONTH) == today[1])
								| (mCalendarStart.get(Calendar.DAY_OF_MONTH) > today[0] & mCalendarStart
										.get(Calendar.MONTH) < today[1])) {
							/**
							 * If start day [i] < today, draw from end time[0]
							 * to 0 AM
							 */
							/**
							 * If data returned is different day, used today, so
							 * need draw from 0:00 AM
							 */
							/**
							 * Check if index start time equal 1440, need reset
							 * index start time back to 0
							 */
							if (index_start_time == 1440) {
								index_start_time = 0;
							}
						} else if ((today[0] < mCalendarEnd
								.get(Calendar.DAY_OF_MONTH) & mCalendarEnd
								.get(Calendar.MONTH) == today[1])
								| (today[0] > mCalendarEnd
										.get(Calendar.DAY_OF_MONTH) & mCalendarEnd
										.get(Calendar.MONTH) > today[1])) {
							/**
							 * If today < end time[i], draw from start time[i]
							 * to 0 AM
							 */
							/**
							 * Check if index start time equal 1440, need break
							 */
							if (index_start_time == 1440) {
								break;
							}
						} else if (mCalendarStart.get(Calendar.DAY_OF_MONTH) == today[0]
								& mCalendarStart.get(Calendar.MONTH) == today[1]) {
							/**
							 * If start day [i] = today, draw from start time[i]
							 * to end time[i]
							 */
							/**
							 * If data returned is same day, so need draw from
							 * specify hours
							 */
						}

						/**
						 * Add sleep data
						 */
						boolean value = (Boolean) data.data.get(i);
						sleep_daily_value[index_start_time] = value;
						if (sleep_daily_value[index_start_time] == true) {
							// Light sleep
							SleepData.mLlSleepDataLightSleep
									.add(sleep_daily_value[i]);
						} else {
							// Deep sleep
							SleepData.mLlSleepDataDeepSleep
									.add(sleep_daily_value[i]);
						}

						index_start_time++;
					}
				}
			}
			break;
		case Key.SLEEP_HISTORY_DATA:
			if (nabuSleepHistory.length == 0) {
				return;
			}

			// First Day has valid data
			Calendar mCalendarFirstDateHasValidData = Calendar.getInstance();
			mCalendarFirstDateHasValidData
					.setTimeInMillis(nabuSleepHistory[0].recordDate * 1000);

			int days = (int) ((Calendar.getInstance().getTimeInMillis() - mCalendarFirstDateHasValidData
					.getTimeInMillis()) / 86400000);

			// Offset
			int offset_from_today = 0;
			while (offset_from_today < days) {
				model.SleepData sleepData = new model.SleepData(null, 0, 0, 0);
				SleepData.sleep_history_data[offset_from_today] = sleepData;

				offset_from_today++;
			}

			for (int i = 0; i < nabuSleepHistory.length; i++) {
				/**
				 * Convert to local time
				 */
				// Day has valid data
				Calendar mCalendarDateHasValidData = Calendar.getInstance();
				mCalendarDateHasValidData
						.setTimeInMillis(nabuSleepHistory[i].recordDate * 1000);

				/**
				 * Object
				 */
				model.SleepData sleepData = new model.SleepData(
						mCalendarDateHasValidData.getTime(),
						nabuSleepHistory[i].good,
						nabuSleepHistory[i].efficiency, nabuSleepHistory[i].bad);

				/**
				 * If current day does not have data, should set that day be
				 * null
				 */
				if (offset_from_today >= 0) {
					SleepData.sleep_history_data[offset_from_today] = sleepData;
				}
				
				// String deep_sleep_minutes =
				// SleepData.sleep_history_data[offset_from_today]
				// .getDeepSleep() + "";
				// String light_sleep_minutes =
				// SleepData.sleep_history_data[offset_from_today]
				// .getLightSleep() + "";
				// Log.i("", offset_from_today + " " +
				// Utils.showDate(SleepData.sleep_history_data[offset_from_today]
				// .getRecordDate()) + " Deep sleep - "
				// + deep_sleep_minutes
				// + " Light Sleep - " +
				// light_sleep_minutes);

				/**
				 * Some days don't have data, so should set them be null, and
				 * skip them also
				 */
				if ((i + 1) == nabuSleepHistory.length) {
					break;
				} else {
					/**
					 * Should calculate offset correct equal to index in array
					 */
					Calendar mCalendarDateHasValidDataBefore = Calendar
							.getInstance();
					mCalendarDateHasValidDataBefore
							.setTimeInMillis(nabuSleepHistory[i + 1].recordDate * 1000);
					if (mCalendarDateHasValidDataBefore.get(Calendar.MONTH) == mCalendarDateHasValidData
							.get(Calendar.MONTH)) {
						offset_from_today += (mCalendarDateHasValidData
								.get(Calendar.DAY_OF_MONTH) - mCalendarDateHasValidDataBefore
								.get(Calendar.DAY_OF_MONTH));
					} else if (mCalendarDateHasValidDataBefore
							.get(Calendar.MONTH) < mCalendarDateHasValidData
							.get(Calendar.MONTH)) {
						long day_in_milliseconds = mCalendarDateHasValidData
								.getTimeInMillis()
								- mCalendarDateHasValidDataBefore
										.getTimeInMillis();

						int day = (int) day_in_milliseconds / 86400000;

						offset_from_today += day;
					}

					if (mCalendarDateHasValidDataBefore.get(Calendar.YEAR) < mCalendarDateHasValidData
							.get(Calendar.YEAR)) {
						offset_from_today++;
					}
				}
			}
			break;
		default:
			break;
		}

	}

	/**
	 * Basic methods
	 */

	public static void clearOldData() {
		SleepData.mLlSdRecordEnd.clear();
		SleepData.mLlSdRecordStart.clear();
		SleepData.mLlSleepDataDeepSleep.clear();
		SleepData.mLlSleepDataLightSleep.clear();
	}

	public static void getSleepData(Context context, Integer... params) {
		GetSleepData.context = context;

		KEY = params[0];
		switch (KEY) {
		case Key.SLEEP_DATA:
			getSleepDailyData();
			break;
		case Key.SLEEP_HISTORY_DATA:
			getSleepHistoryData();
			break;
		default:
			break;
		}
	}

	private static void getSleepDailyData() {
		/**
		 * Get Start time & End time
		 */
		// Start time
		Calendar mCalendarBegin = Calendar.getInstance();
		mCalendarBegin.setTime(DashBoardFragment.singleTon
				.getCurrentDateBegin());
		// End time
		Calendar mCalendarEnd = Calendar.getInstance();
		mCalendarEnd.setTime(DashBoardFragment.singleTon.getCurrentDateEnd());

		long start_time = mCalendarBegin.getTimeInMillis();
		long end_time = mCalendarEnd.getTimeInMillis();

		/**
		 * Sleep Data
		 */
		if (context == null) {
			context = ActivityFitness.mContextActivityFitness;
		}
		if (Constants.nabuSDK == null) {
			Constants.nabuSDK = NabuOpenSDK.getInstance(context);
		}
		Constants.nabuSDK.getSleepTrackerData(context, start_time / 1000,
				end_time / 1000, new SleepTrackerListener() {

					@Override
					public void onReceiveData(NabuSleepTracker[] sleepData) {
						Log.i("", "getSleepTrackerData - sleepData.length --- "
								+ sleepData.length);

						KEY = Key.SLEEP_DATA;
						addSleepValue(sleepData, null);

						getSleepDailyTimeValueOnChart();
						showSleepDataOnPage(context);

						if (DashBoardFragment.iTaskCallBacks != null) {
							DashBoardFragment.iTaskCallBacks
									.onCompleteReceiveData();
						}
					}

					@Override
					public void onReceiveFailed(String errorMessage) {
						Log.i("", "getSleepTrackerData - onReceiveFailed "
								+ errorMessage);

						/**
						 * If get invalid token, need terminate the application
						 */
						if (errorMessage.equals(Constants.INVALID_TOKEN)) {
							/**
							 * Deleted cache before exit
							 */
							Utils.clearApplicationData(context);

							try {
								Thread.sleep(2000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}

							android.os.Process.killProcess(android.os.Process
									.myPid());
						}
					}
				});
	}

	private static void getSleepDailyTimeValueOnChart() {
		/**
		 * Length of chart
		 */
		int[] x_sleep = new int[1440];
		/**
		 * Get current day to compare
		 */
		int[] today = getTodayFromCurrentDate();

		/**
		 * Get final hours to show on page
		 */
		int final_deep_sleep_hours = 0;
		int final_deep_sleep_minutes = 0;

		int final_light_sleep_hours = 0;
		int final_light_sleep_minutes = 0;

		int final_total_sleep_hours = 0;
		int final_total_sleep_minutes = 0;

		/**
		 * Begin draw on chart
		 */
		int index_deep_sleep = 0;
		int index_light_sleep = 0;

		int index_increased_in_size_today = 0;
		int index_today = 0;
		int record_end_time = 0;
		for (Long recordStartTime : SleepData.mLlSdRecordStart) {
			// Start time
			Calendar mCalendarStart = Calendar.getInstance();
			mCalendarStart.setTimeInMillis(recordStartTime * 1000);

			// get Hours and Minutes of start time
			int hours_start_time = mCalendarStart.get(Calendar.HOUR_OF_DAY);
			int minutes_start_time = mCalendarStart.get(Calendar.MINUTE);

			// End time
			Calendar mCalendarEnd = Calendar.getInstance();
			mCalendarEnd.setTimeInMillis(SleepData.mLlSdRecordEnd
					.get(record_end_time) * 1000);
			record_end_time++;
			// } else if ((today[0] < mCalendarEnd.get(Calendar.DAY_OF_MONTH)
			// & mCalendarEnd.get(Calendar.MONTH) == today[1])
			// | ( today[0] > mCalendarEnd.get(Calendar.DAY_OF_MONTH)
			// & mCalendarEnd.get(Calendar.MONTH) > today[1])) {
			// Log.i("", "today[0] " + today[0]);
			// Log.i("", "DAY_OF_MONTH " +
			// mCalendarEnd.get(Calendar.DAY_OF_MONTH));
			// Log.i("", "today[1] " + today[1]);
			// Log.i("", "mCalendarEnd.get(Calendar.MONTH) " +
			// mCalendarEnd.get(Calendar.MONTH));
			if ((mCalendarStart.get(Calendar.DAY_OF_MONTH) < today[0] & mCalendarStart
					.get(Calendar.MONTH) == today[1])
					| (mCalendarStart.get(Calendar.DAY_OF_MONTH) > today[0] & mCalendarStart
							.get(Calendar.MONTH) < today[1])) {
				// Log.i("", "start day [i] < today");

				/**
				 * If start day [i] < today, draw from end time[0] to 0 AM
				 */
				/**
				 * If data returned is different day, used today, so need draw
				 * from 0:00 AM
				 */
				int hours_end_time = mCalendarEnd.get(Calendar.HOUR_OF_DAY);
				int minutes_end_time = mCalendarEnd.get(Calendar.MINUTE);

				// Get index to show on chart
				int index_end_time = hours_end_time * 60 + minutes_end_time;

				/**
				 * Get index size
				 */
				int index_size = 0;
				for (int i = 0; i < size_starttime_today.length; i++) {
					if (size_starttime_today[i] > 0) {
						index_size += size_starttime_today[i];
					}
				}

				/**
				 * Begin get sleep time
				 */
				int deep_sleep = 0;
				int light_sleep = 0;
				for (int i = index_size - 1; i >= 0; i--) {
					if (index_end_time < 0) {
						break;
					}

					if (sleep_daily_value[index_end_time] == true) {
						// Light Sleep
						light_sleep++;
					} else {
						// Deep Sleep
						deep_sleep++;
					}

					index_end_time--;
				}

				/**
				 * Get final hours, minutes to show on page
				 */
				final_deep_sleep_hours += deep_sleep / 60;
				final_deep_sleep_minutes += deep_sleep % 60;

				final_light_sleep_hours += light_sleep / 60;
				final_light_sleep_minutes += light_sleep % 60;

				final_total_sleep_hours += (final_deep_sleep_hours + final_light_sleep_hours);
				final_total_sleep_minutes += (final_deep_sleep_minutes + final_light_sleep_minutes);

				index_increased_in_size_today++;
			} else if ((today[0] < mCalendarEnd.get(Calendar.DAY_OF_MONTH) & mCalendarEnd
					.get(Calendar.MONTH) == today[1])
					| (today[0] > mCalendarEnd.get(Calendar.DAY_OF_MONTH) & mCalendarEnd
							.get(Calendar.MONTH) > today[1])) {
				// Log.i("", "today < end time[i]");

				/**
				 * If today < end time[i], draw from start time[i] to 0 AM
				 */
				// Get index to get data from sleep daily
				int index_start_time = hours_start_time * 60
						+ minutes_start_time;

				int hours_middle = (x_sleep.length - 1 - index_start_time) / 60;
				int mitues_middle = (x_sleep.length - 1 - index_start_time) % 60;

				/**
				 * Get final hours, minutes to show on page
				 */
				final_total_sleep_hours += hours_middle;
				final_total_sleep_minutes += mitues_middle;

				/**
				 * Get index size
				 */
				int index_size = 0;
				for (int i = 0; i < size_today_endtime.length; i++) {
					if (size_today_endtime[i] > 0) {
						index_size += size_today_endtime[i];
					}
				}

				/**
				 * Begin get sleep time
				 */
				int deep_sleep = 0;
				int light_sleep = 0;
				for (int i = 0; i < index_size; i++) {
					if (index_start_time >= x_sleep.length) {
						break;
					}

					if (sleep_daily_value[index_start_time] == true) {
						// Light Sleep
						light_sleep++;
					} else {
						// Deep Sleep
						deep_sleep++;
					}

					index_start_time++;
				}

				/**
				 * Get final hours, minutes to show on page
				 */
				final_deep_sleep_hours += deep_sleep / 60;
				final_deep_sleep_minutes += deep_sleep % 60;

				final_light_sleep_hours += light_sleep / 60;
				final_light_sleep_minutes += light_sleep % 60;

				final_total_sleep_hours += (final_deep_sleep_hours + final_light_sleep_hours);
				final_total_sleep_minutes += (final_deep_sleep_minutes + final_light_sleep_minutes);

				index_increased_in_size_today++;
			} else if (mCalendarStart.get(Calendar.DAY_OF_MONTH) == today[0]
					& mCalendarStart.get(Calendar.MONTH) == today[1]) {
				// Log.i("", "start day [i] = today");

				/**
				 * If start day [i] = today, draw from start time[i] to end
				 * time[i]
				 */
				/**
				 * If data returned is same day, so need draw from specify hours
				 */
				/**
				 * Index of chart view
				 */
				int index_start_time = hours_start_time * 60
						+ minutes_start_time;

				/**
				 * Check if index in size today > 1, reset sleep index = 0
				 */
				if (index_increased_in_size_today > 1) {
					// Reset index to get Sleep Data from 0 to get exactly sleep
					// value
					index_deep_sleep = 0;
					index_light_sleep = 0;
				}
				for (int i = 0; i < size_today[index_increased_in_size_today]; i++) {
					if (sleep_daily_value[index_start_time] == true) {
						// Light Sleep
						index_light_sleep++;
					} else {
						// Deep Sleep
						index_deep_sleep++;
					}

					index_start_time++;
				}

				// Assign last index
				index_today++;
				index_increased_in_size_today++;

				/**
				 * If is the last size, get sleep data
				 */
				if (index_today == size_actual_today | size_actual_today == 0
						| size_actual_today == 1) {
					/**
					 * Get final hours, minutes to show on page
					 */
					final_deep_sleep_hours += index_deep_sleep / 60;
					final_deep_sleep_minutes += index_deep_sleep % 60;

					final_light_sleep_hours += index_light_sleep / 60;
					final_light_sleep_minutes += index_light_sleep % 60;

					final_total_sleep_hours += (final_deep_sleep_hours + final_light_sleep_hours);
					final_total_sleep_minutes += (final_deep_sleep_minutes + final_light_sleep_minutes);
				}
			}
		}

		/**
		 * Calculate total hours again to show on page
		 */
		saveSleepValueIntoSingleTon(final_deep_sleep_hours,
				final_deep_sleep_minutes, final_light_sleep_hours,
				final_light_sleep_minutes, final_total_sleep_hours,
				final_total_sleep_minutes);
	}

	private static void getSleepHistoryData() {
		Constants.nabuSDK.getSleepHistoryData(context, 365,
				new SleepHistoryListener() {

					@Override
					public void onReceiveData(
							NabuSleepHistory[] nabuSleepHistory) {
						Log.i("", "getSleepHistoryData - sleepData.length --- "
								+ nabuSleepHistory.length);

						KEY = Key.SLEEP_HISTORY_DATA;
						addSleepValue(null, nabuSleepHistory);
					}

					@Override
					public void onReceiveFailed(String errorMessage) {
						Log.i("", "onReceiveFailed " + errorMessage);

						/**
						 * If get invalid token, need terminate the application
						 */
						if (errorMessage.equals(Constants.INVALID_TOKEN)) {
							/**
							 * Deleted cache before exit
							 */
							Utils.clearApplicationData(context);

							try {
								Thread.sleep(2000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}

							android.os.Process.killProcess(android.os.Process
									.myPid());
						}
					}
				});
	}

	private static int[] getTodayFromCurrentDate() {
		String CURRENT_DAY = DashBoardFragment.singleTon.getCurrentDate();
		int today[] = new int[2];

		// Log.i("", "CURRENT_DAY " + CURRENT_DAY);
		/**
		 * Check language in System Settings to get today
		 */
		// switch (Utils.checkLanguageInSettings()) {
		// case Language.CHINESE:
		String[] current_day = CURRENT_DAY.split(" ");
		if (CURRENT_DAY.contains(context.getString(R.string.today))) {
			today[0] = Integer.valueOf(current_day[1]);
			today[1] = Utils.getCorrectIndexMonth(context, current_day[2]);
		} else {
			today[0] = Integer.valueOf(current_day[0]);
			today[1] = Utils.getCorrectIndexMonth(context, current_day[1]);
		}
		// break;
		// case Language.ENGLISH:
		// String[] current_day = CURRENT_DAY.split(" ");
		// if (CURRENT_DAY.contains(context.getString(R.string.today))) {
		// today = Integer.valueOf(current_day[1]);
		// } else {
		// today = Integer.valueOf(current_day[0]);
		// }
		// break;
		// }

		return today;
	}

	private static void saveSleepValueIntoSingleTon(int final_deep_sleep_hours,
			int final_deep_sleep_minutes, int final_light_sleep_hours,
			int final_light_sleep_minutes, int final_total_sleep_hours,
			int final_total_sleep_minutes) {
		// Deep sleep
		DashBoardFragment.singleTon.setDeepSleepHours(final_deep_sleep_hours
				+ "");
		DashBoardFragment.singleTon
				.setDeepSleepMinutes(final_deep_sleep_minutes + "");

		// Light sleep
		DashBoardFragment.singleTon.setLightSleepHours(final_light_sleep_hours
				+ "");
		DashBoardFragment.singleTon
				.setLightSleepMinutes(final_light_sleep_minutes + "");

		// Total sleep
		DashBoardFragment.singleTon.setTotalSleepHours(final_total_sleep_hours
				+ "");
		DashBoardFragment.singleTon
				.setTotalSleepMinutes(final_total_sleep_minutes + "");

		/**
		 * Need save today sleep value into single ton
		 */
		if (DashBoardFragment.IS_FIRST_TIME_DOWNLOAD_DATA_TODAY_SLEEP) {
			DashBoardFragment.IS_FIRST_TIME_DOWNLOAD_DATA_TODAY_SLEEP = false;
			if (DashBoardFragment.singleTon.getCurrentDate().contains(
					context.getString(R.string.today))) {
				DashBoardFragment.singleTon
						.setSleepTodayDeepHours(final_deep_sleep_hours + "");
				DashBoardFragment.singleTon
						.setSleepTodayDeepMinutes(final_deep_sleep_minutes + "");

				DashBoardFragment.singleTon
						.setSleepTodayLightHours(final_light_sleep_hours + "");
				DashBoardFragment.singleTon
						.setSleepTodayLightMinutes(final_light_sleep_minutes
								+ "");

				int sleep_mintues = final_total_sleep_hours / 60
						+ final_total_sleep_minutes;
				DashBoardFragment.singleTon.setSleepTodayTotal(sleep_mintues
						+ "");
			} else {
				DashBoardFragment.singleTon.setSleepTodayDeepHours("0");
				DashBoardFragment.singleTon.setSleepTodayDeepMinutes("0");
				DashBoardFragment.singleTon.setSleepTodayLightHours("0");
				DashBoardFragment.singleTon.setSleepTodayLightMinutes("0");
				DashBoardFragment.singleTon.setSleepTodayTotal("0");
			}
		}
	}

	private static void showSleepDataOnPage(Context context) {
		/**
		 * If current page is DashBoard set Sleep data value for text view
		 */
		// Dash Board page
		if (DashBoard.mFmDashBoard instanceof DashBoardFragment) {
			int sleep_hours = Integer.valueOf(DashBoardFragment.singleTon
					.getDeepSleepHours())
					+ Integer.valueOf(DashBoardFragment.singleTon
							.getLightSleepHours());
			int sleep_minutes = Integer.valueOf(DashBoardFragment.singleTon
					.getDeepSleepMinutes())
					+ Integer.valueOf(DashBoardFragment.singleTon
							.getLightSleepMinutes());

			// Save deep sleep hours, light sleep hours, total sleep hours
			// into single ton
			DashBoardFragment.singleTon.setTotalSleepHours("" + sleep_hours);
			DashBoardFragment.singleTon
					.setTotalSleepMinutes("" + sleep_minutes);

			/**
			 * Update UI
			 */
			DashBoardFragment.updateSleepDataOnPage(context,
					DashBoardFragment.singleTon.getTotalSleepHours(),
					DashBoardFragment.singleTon.getTotalSleepMinutes(),
					DashBoardFragment.singleTon.getDeepSleepHours(),
					DashBoardFragment.singleTon.getDeepSleepMinutes(),
					DashBoardFragment.singleTon.getLightSleepHours(),
					DashBoardFragment.singleTon.getLightSleepMinutes(),
					DashBoardFragment.singleTon.getSleepGoal());
		}

		// Dash Board Detail page
		if (DashBoardDetail.mFmDashBoardDetail instanceof DashBoardDetailFragment) {
			DashBoardDetailFragment.updateSleepDataOnPage(context,
					DashBoardFragment.singleTon.getTotalSleepHours(),
					DashBoardFragment.singleTon.getTotalSleepMinutes(),
					DashBoardFragment.singleTon.getDeepSleepHours(),
					DashBoardFragment.singleTon.getDeepSleepMinutes(),
					DashBoardFragment.singleTon.getLightSleepHours(),
					DashBoardFragment.singleTon.getLightSleepMinutes(),
					DashBoardFragment.singleTon.getSleepGoal());
		}
	}
}
